package com.zhao.modleclass.modle;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.zhao.modleclass.adapter.BiaoQianAdapter;
import com.zhao.modleclass.bean.SC_TeacherRActivityBean;
import com.zhao.modleclass.bean.SearchClassBiaoQianBean;
import com.zhao.modleclass.bean.SearchClassItemBean;
import com.zhao.modleclass.interfaces.IViewPager;
import com.zhao.modleclass.utils.Constant;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by 张昭 on 2016/11/29.
 */

public class SearchModle implements ISearchModle {
    private IViewPager iviewPager;
    @Override
    public void  login( String type, Map<String, String> map,  final OnLoadSearchListener loadSearchListener,int tag) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.SearchClass_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        iviewPager = retrofit.create(IViewPager.class);
        Observable<SearchClassBiaoQianBean> observable = iviewPager.getJsonSearchClass3String(type,map);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<SearchClassBiaoQianBean>() {
                    @Override
                    public void call(SearchClassBiaoQianBean searchClassBiaoQianBean) {
                        loadSearchListener.onSucess(searchClassBiaoQianBean);

                    }
                });
    }

    @Override
    public void loginTeacher(String type, Map<String, String> map, final OnLoadSearchListener loadSearchListener,int tag) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.SearchClassTeacher_URL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        iviewPager = retrofit.create(IViewPager.class);
        Observable<SearchClassItemBean> observable = iviewPager.getJsonSearchClassItemString(type, map);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SearchClassItemBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(SearchClassItemBean searchClassItemBean) {
                        loadSearchListener.onSucessTeacher(searchClassItemBean);
                    }
                });
    }

    @Override
    public void loginTeacherActivity(String type, Map<String, String> map, final OnLoadSearchListener loadSearchListener,int tag) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constant.SearchClass_RURL)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        iviewPager = retrofit.create(IViewPager.class);
        Observable<SC_TeacherRActivityBean> observable = iviewPager.getJsonSearchClassSCTRAString(type, map);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<SC_TeacherRActivityBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.i("TAG", "错误是+onError: +++++++>>>>>>"+e.toString());
                    }

                    @Override
                    public void onNext(SC_TeacherRActivityBean sc_teacherRActivityBean) {
                        loadSearchListener.onSucessMoreTag(sc_teacherRActivityBean);
                    }
                });
    }
}
